package com.cn.learn.jpa.service.impl;

import com.cn.learn.jpa.domain.jpa.Game;
import com.cn.learn.jpa.domain.model.ResultModel;
import com.cn.learn.jpa.repository.GameRepository;
import com.cn.learn.jpa.service.IGameService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName GameServiceImpl
 * @Description
 * @Author JiJiang
 * @Date 2023/7/1 22:58
 * @Version 1.0
 */

@Log4j2
@Service
public class GameServiceImpl implements IGameService {


    @Resource
    private GameRepository gameRepository;


    /**
     * 保存游戏
     *
     * @param toAdd
     * @return
     */
    @Override
    public ResultModel saveGame(Game toAdd) {


        Map<String, Object> retData = new HashMap<>();

        int affectedRow = 0;
        Game afterSave = null;
        try {

            afterSave = gameRepository.save(toAdd);
            affectedRow = 1;
        } catch (Exception e) {

            log.error("类：{} 执行方法：{} 时出现异常！原因如下：{}", this.getClass().getName(), "saveGame", e.getMessage());
        }

        retData.put("affectedRow", affectedRow);
        retData.put("savedData", afterSave);

        return ResultModel.getResultModel(retData);
    }
}
